package utils;

import android.os.CountDownTimer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import utils.log.LogUtil;

public class TimerUtil extends CountDownTimer {

    private OnBaseTimerCallBack onBaseTimerCallBack;

    /**
     * @param millisInFuture    时间间隔是多长时间
     * @param countDownInterval 回调onTick方法，每隔多久执行一次
     */
    public TimerUtil(long millisInFuture, long countDownInterval, OnBaseTimerCallBack onBaseTimerCallBack) {
        super(millisInFuture, countDownInterval);
        this.onBaseTimerCallBack = onBaseTimerCallBack;
    }

    //间隔时间内执行的操作
    @Override
    public void onTick(long millisUntilFinished) {
        onBaseTimerCallBack.onTick(millisUntilFinished);
    }

    //间隔时间结束的时候才会调用
    @Override
    public void onFinish() {
        onBaseTimerCallBack.onFinish();
    }

    /**
     * 图片二点击回调接口
     */
    public interface OnBaseTimerCallBack {
        /**
         * 间隔时间内执行的操作
         *
         * @param millisUntilFinished 距离结束剩余时间
         */
        void onTick(long millisUntilFinished);

        /**
         * 间隔时间结束的时候才会调用
         */
        void onFinish();
    }

    public static String stringForTime(long position) {
        int totalSeconds = (int) ((position / 1000.0) + 0.5);

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return String.format(Locale.US, "%02d:%02d:%02d", hours, minutes, seconds).toString();
    }

    public static int GetHours(String start, String end) {
        //格式化时间
        SimpleDateFormat CurrentTime = new SimpleDateFormat("HH:mm");

        //取得系统时间
        long time = System.currentTimeMillis();
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        //取得小时：
        int mHour = mCalendar.get(Calendar.HOUR);
        return mHour;
    }

    public static List<String> GetlistDate() {
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();  //获取当前时间，
        String year = calendar.get(Calendar.YEAR) + "";
        int month = calendar.get(Calendar.MONTH);
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                list.add(year + "-0" + i);
            } else {
                list.add(year + "-" + i);
            }
        }
        Collections.swap(list,0,month);
        return list;
    }

    public static List<String> GetlistMonthAndDayDate() {
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();  //获取当前时间，
        String year = calendar.get(Calendar.YEAR) + "";
        int month = calendar.get(Calendar.MONTH) + 1;
        String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
        for (int i = month; i >= 1; i--) {
            if (i < 10) {
                list.add(year + "-0" + i);
            } else {
                list.add(year + "-" + i);
            }
        }
        return list;
    }

    public static List<String> GetlistWeekAndDayDate(int month) {
        List<String> list = new ArrayList<>();
        Date datenow = new Date();
//        Calendar calendar = Calendar.getInstance();  //获取当前时间，
//        Calendar mcalendar = Calendar.getInstance();
//        String year = calendar.get(Calendar.YEAR) + "";
//        int cmonth = calendar.get(Calendar.MONTH) + 1;
//        int Day = calendar.get(Calendar.DAY_OF_MONTH);
//        int totalday = getMonthLastDay(Integer.parseInt(year), month);
//        calendar.setTime(datenow);
        SimpleDateFormat CurrentTime = new SimpleDateFormat("MM-dd");
        for (int i = 1; i < 30; i++) {
            Calendar calendar = Calendar.getInstance();  //获取当前时间，
            Calendar mcalendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -(6 * i));
            mcalendar.add(Calendar.DAY_OF_MONTH, -(6 * (i - 1)));
            String mdate = CurrentTime.format(mcalendar.getTime());
            String date = CurrentTime.format(calendar.getTime());
            LogUtil.app("before---->" + mdate + "--" + date);
            list.add(date + "--" + mdate);
        }
//        String monthstr = "";
//        if(month<10){
//            monthstr = "0"+month;
//        }
//        //year+"-"
//        String str = monthstr+"-01";
//        String str1 = monthstr+"-07";
//        String str2 = monthstr+"-14";
//        String str3 = monthstr+"-21";
//        String str4 = monthstr+"-28";
//        if(1<=Day && Day<=7){
//            str1 = monthstr+"-0"+Day;
//            list.add(str+"---"+str1);
//        }else if(7<Day && Day<=14){
//            str2 = monthstr+"-"+Day;
//            list.add(str1+"---"+str2);
//            list.add(str+"---"+str1);
//        }else if(14<Day && Day<=21){
//            str3 =  monthstr+"-"+Day;
//            list.add(str2+"---"+str3);
//            list.add(str1+"---"+str2);
//            list.add(str+"---"+str1);
//        }else if(Day>21){
//            str4 =  monthstr+"-"+Day;
//            list.add(str3+"---"+str4);
//            list.add(str2+"---"+str3);
//            list.add(str1+"---"+str2);
//            list.add(str+"---"+str1);
//        }
        return list;
    }

    /**
     * 得到指定月的天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 小时转换获取秒
     */
    public static int GetS(String s) {
        int i = 0;
        String[] str = s.split(":");
        if (!StringUtils.isNull(str[0])) {
            i = Integer.parseInt(str[0])*3600;
        }
        if (!StringUtils.isNull(str[1])) {
            i =i + Integer.parseInt(str[1])*60;
        }
        if (!StringUtils.isNull(str[2])) {
            i =i + Integer.parseInt(str[2]);
        }
        return i*1000;
    }
}
